#include <iostream>
using std::cout;
using std::cin;
using std::endl;
class Singleton {
public://公有成员函数
    static Singleton* getInstance() {
        if (ms_instance == nullptr) {
            ms_instance = new Singleton{};
            atexit(destroyInstance);
        }
        return ms_instance;
    }
private://私有成员函数
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;
    Singleton(int val = 1)
        :m_val(val)
    {
        cout << "Singleton(int val = 1)" << endl;
    }
    ~Singleton() {
        cout << "~Singleton()" << endl;
    }
    static void destroyInstance() {
        if (ms_instance) {
            delete ms_instance;
            ms_instance = nullptr;
            cout << "Release over" << endl;
        }
    }
private://私有数据成员
    int m_val;
    static Singleton* ms_instance;
};//end of class Singleton
//静态数据成员初始化
//动态初始化，饿汉式
Singleton* Singleton::ms_instance = Singleton::getInstance();
void test() {
    cout << Singleton::getInstance() << endl;
    cout << Singleton::getInstance() << endl;
    cout << Singleton::getInstance() << endl;
}
int main() {
    cout << "before" << endl;
    test();
    cout << "after" << endl;
    return 0;
}